package controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import dao.LogDao;
import pojo.Users;
import service.UserLogService;
import service.UsersService;

@Controller
public class UsersController {
	//登陆
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		    //System.out.println(password);
		    UsersService service = new UsersService();
		    UserLogService service1 = new UserLogService();
		    Users result = service.query(account, password);
		    if ("0".equals(result.getCounter())) {
			return "ERR";}
		    response.addCookie(new Cookie("user", String.valueOf(result.getId())));
		    service1.adduser_log(String.valueOf(result.getId()),"0","登陆");
		    return "OK";
	}
    //昵称显示
	@RequestMapping(value = "/querynickname.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> querynickname(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		    UsersService service = new UsersService();
		    Map<String, Object> result = service.querynickname(userId);
		    return result;
	}
    //退出登录
	@RequestMapping(value = "/logout.action", method = RequestMethod.POST)
	@ResponseBody
	public String logout(
			@CookieValue(value = "user", defaultValue = "") String userId,
			HttpServletResponse response) {
		    UserLogService service1 =new UserLogService();
		    service1.adduser_log(userId,"0","退出登录");
		    response.addCookie(new Cookie("user", null));
		    return "OK";
	}
}
